package com.xzz.mq.producer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xzz.domain.MediaFile;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class MediaProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public boolean sendMessage(MediaFile mediaFile){
        //可以用阿里的json object，任意类转string
        String jsonString = JSON.toJSONString(mediaFile);
        //构建消息
        Message<String> message = MessageBuilder.withPayload(jsonString).build();
        //发送同步消息
        SendResult sendResult = rocketMQTemplate.syncSend("message:sms", message);

        SendStatus status = sendResult.getSendStatus();
        return status.equals(status.SEND_OK);
    }

}
